#include "SeaWinKNXDevice.h"
#include <unistd.h>
#include "Main/GateWay.h"
SeaWinKNXDevice::SeaWinKNXDevice(QObject *parent) :
    BaseInterface<SeaWinKNXDevice>(parent)
{
    qDebug()<<"this is SeaWinKNXDevice constructor";
     m_pInstance = this;
    swKNXProtocol = new SWKNXProtocol(this);

    this->registerProtocol(swKNXProtocol);

    /*getMacTimer = new QTimer(this);

    connect(getMacTimer, SIGNAL(timeout()), this, SLOT(getMacSlot()));

    getMacTimer->setInterval(30*1000);
    getMacTimer ->start();*/

   // QElapsedTimer delayTimer;
  //  delayTimer.start();
  //  while(delayTimer.elapsed()<=10*1000);

     qDebug()<<"SeaWinKNXDevice m_portList-"<<m_portList.size();
    foreach (const BasePort *port , m_portList)
    {

         if((port->portParam().type() == "UART" && port->portParam().name() == "SWKNX"))
         {
             m_target = port->portParam();
         }
    }


    // registerCmdHandler(ackKNXMacCmd::FunctionCode,ackKNXMacCmd::staticMetaObject, &SWKNXDevice::ackKNXMacCmdHandle);
      registerCmdHandler(ackKNXStatusCmd::FunctionCode,ackKNXStatusCmd::staticMetaObject, &SeaWinKNXDevice::ackKNXStatusCmdHandle);
    registerFuncHandler(ControlPointFunc::FunctionCode, &SeaWinKNXDevice::getMacSlot1);
//getMacSlot();

}

void SeaWinKNXDevice:: ackKNXStatusCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    qDebug()<<"~~~~~~~~~~~~~~SeaWinKNXDevice::ackKNXStatusCmdHandle11";
    /*if(heartbeatTimer != NULL)
    {
         qDebug()<<"~~~~~~~~~~~~~~SWKNXDevice::ackKNXStatusCmdHandle0000";
    heartbeatTimer->setInterval(500);

    heartbeatTimer->setSingleShot(true);
    heartbeatTimer ->start();
    }
    else
    {
        qDebug()<<"heartbeatTimer is NULL";
    }*/


   PortStruct target;

   qDebug()<<"SWKNXDevice::writeC3-1";
   QByteArray data;
   data.append(0xC3);
   qDebug()<<"SWKNXDevice::writeC3-2";
   emit sendDataSignal(m_target,data);
   qDebug()<<"SWKNXDevice::writeC3-3";
    qDebug()<<"~~~~~~~~~~~~~~SWKNXDevice::ackKNXStatusCmdHandle222";
    ackKNXStatusCmd * _ackKNXStatusCmd = qobject_cast<ackKNXStatusCmd*>(sharedCmd.data());
    System *system = System::getInstance();
    system->setKNXStatus(_ackKNXStatusCmd->getKNXStatus());

}

void SeaWinKNXDevice::getMacSlot1(QSharedPointer<BaseFunc> sharedFunc)
{
    qDebug()<<"SeaWinKNXDevice::getMacSlot1()";
    qDebug()<<"getMacSlot m_portList-"<<m_portList.size();
  //  getKNXMacCmd *mm = new getKNXMacCmd();
    getKNXStatusCmd * mm = new getKNXStatusCmd();
    QSharedPointer<BaseCmd> sharedSyncCmd(mm);

    PortStruct target;
    qDebug()<<"getMacSlot m_portList-"<<m_portList.size();
    foreach (const BasePort *port , m_portList)
    {
         if((port->portParam().type() == "UART" && port->portParam().name() == "SWKNX"))
         {
             target = port->portParam();
         }
    }
    this->sendCmd(target,sharedSyncCmd);
}

void SeaWinKNXDevice::getMacSlot()
{
    qDebug()<<"SeaWinKNXDevice::getMacSlot()";
    qDebug()<<"getMacSlot m_portList-"<<m_portList.size();
  //  getKNXMacCmd *mm = new getKNXMacCmd();
    getKNXStatusCmd * mm = new getKNXStatusCmd();
    QSharedPointer<BaseCmd> sharedSyncCmd(mm);

    PortStruct target;
    qDebug()<<"getMacSlot m_portList-"<<m_portList.size();
    foreach (const BasePort *port , m_portList)
    {
         if((port->portParam().type() == "UART" && port->portParam().name() == "SWKNX"))
         {
             target = port->portParam();
         }
    }
    this->sendCmd(target,sharedSyncCmd);
}
